/**
 * Created by DELL on 2017/5/10.
 */
common.buildNormalEnumSelect(".rewardStatus", enumObject.enumJsonData['rewardStatus']);//创建悬赏状态枚举列表
/**
 * 中介联系人 列表初始化设置
 * @type {Object}
 */
var settings = {
    "sAjaxSource": window.globalHost + common.rewardHouseListPath,
    "aoColumns": [{
        "mDataProp": function(obj) {
            var status=enumObject.enumJsonData["rewardStatus"][obj.rewardStatus];
            return "<a href='../submenu.html?key=rewardHouse&id=" + obj.id + "&houseId="+obj.houseId+"'>" + (status?status:"无状态") + "</a>";
        },
        "sTitle": "房源状态",
        "sDefaultContent": "",
        "sClass": "text-center"
    },{
        "mDataProp": function(obj) {
            return "<a href='../submenu.html?key=rewardHouse&id=" + obj.id + "&houseId="+obj.houseId+"'>" + (obj.contractNo?obj.contractNo:"无编号") + "</a>";
        },
        "sTitle": "签约合同编号",
        "sDefaultContent": "",
        "sClass": "text-center"
    },{
        "mDataProp": function(obj) {
            return !!obj.villageName?obj.villageName:"无信息";
        },
        "sTitle": "小区名称",
        "sDefaultContent": ""
    },{
        "mDataProp": function(obj) {
            return obj.houseAddress?obj.houseAddress:"无信息";
        },
        "sTitle": "房源地址",
        "sDefaultContent": ""
    },{
        "mDataProp": function(obj) {
            return obj.quotePrice?obj.quotePrice:"无信息";
        },
        "sTitle": "报价(万元)",
        "sDefaultContent": ""
    },{
        "mDataProp": function(obj) {
            return obj.viewTimes?obj.viewTimes:"无信息";
        },
        "sTitle": "查看",
        "sDefaultContent": "",
        "sClass":"text-center"
    }, {
        "mDataProp": "createTime",
        "sTitle": "创建时间",
        "sDefaultContent": ""
    }, {
        "mDataProp": function (obj) {
            var recommendBtn="";
            if(obj.isRecommended=="0"){
                recommendBtn="<a href='javascript:;' class='btn btn-success col-xs-5 pull-right set-recommend' data-id='"+obj.id+"'>设置推荐</a>";
            }else if(obj.isRecommended=="1"){
                recommendBtn="<a href='javascript:;' class='btn btn-danger col-xs-5 pull-right recommended' data-id='"+obj.id+"'>已推荐房源</a>";
            }
            if(obj.rewardStatus=="1"){
                return "<a href='javascript:;' class='btn btn-success col-xs-5 change-rewardStatus' data-id='"+obj.id+"' data-status='"+obj.rewardStatus+"'>修改状态</a>"+recommendBtn;
            }else{
                return "<a href='javascript:;' class='btn btn-success col-xs-6 col-xs-offset-3 change-rewardStatus' data-id='"+obj.id+"' data-status='"+obj.rewardStatus+"'>修改状态</a>";
            }
        },
        "sTitle": "操作",
        "sDefaultContent": "",
        sClass:"clearfix text-center"
    }],
    "fnServerParams": function(aoData) {
        return common.getSearchData("form.data-list-search",aoData);
    }
};
var options = $.extend(true, {}, _dataTableSettings, settings);
/**
 * 实例化 中介联系人 列表插件
 * @type {[type]}
 */
var $currentDataTable=$("#data-list");
var oTable = $currentDataTable.dataTable(options);
searchList.getDataList(oTable);
searchList.resetSearch(oTable);

var startRewardDom='<div class="form-group" style="margin-top: 10px">'+
    '<label class="col-sm-2 control-label">悬赏状态</label>'+
    '<div class="col-sm-8 otherInfo no-plr houseOrientations">'+
    '<input type="radio" value="1" name="rewardStatus" id="rewardStatus1">'+
    '<label for="rewardStatus1" style="line-height:1">悬赏中</label>'+
    '<input type="radio" value="2" name="rewardStatus" id="rewardStatus2">'+
    '<label for="rewardStatus2" style="margin: 0 10px;line-height:1">已完成</label>'+
    '<input type="radio" value="3" name="rewardStatus" id="rewardStatus3">'+
    '<label for="rewardStatus3" style="margin: 0 10px;line-height:1">已取消</label>'+
    '</div>'+
    '</div>';
$currentDataTable.off("click",".change-rewardStatus").on("click",".change-rewardStatus",function (e) {
    var RewardId=$(this).attr("data-id");
    var houseStatus=$(this).attr("data-status");
    var sendData={};
    common.openTempDialog({
        title: '更新悬赏状态',
        area:['640px', '150px'],
        content: startRewardDom,
        FailedMsg:"请选择悬赏状态",
        sendData:sendData,
        ajaxUrl:window.globalHost+common.rewardStatusUpdatePath,
        contentOnLoad:function (layerDocument) {
            //弹窗内控件加载初始化
            var radios=layerDocument.find("input[type='radio']");
            $.each(radios,function (idx, ele) {
               if(ele.value==houseStatus){
                   ele.checked=true;
                   return;
               }
            });
        },
        beforeSend:function (layerDocument) {
            var newStatusVal=layerDocument.find(":checked").val();
            sendData.id=RewardId;
            sendData.rewardStatus=newStatusVal;
            return sendData.id && sendData.rewardStatus;
        },
        resOk:function () {
            //如果更新成功，刷新表格数据
            oTable.fnClearTable();
        }
    });
});
$currentDataTable.off("click",".set-recommend").on("click",".set-recommend",function (e) {
    var rewardId=$(this).attr("data-id");
    common.confirm({msg:"是否确定发起推荐?"}).then(function(){
        $.post(window.globalHost +common.recommendHousePath, {id:rewardId}).success(function (rst) {
            layer.alert(rst.res.msg);
            if (rst.code === "0") {
                oTable.fnClearTable();
            }
        });
    });
});